package com.vision.bigDataApi.controller.mysql;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jgoodies.binding.beans.Model;
import com.vision.bigDataApi.enums.ReturnStatus;
import com.vision.bigDataApi.mapper.mysql.UserMapper;
import com.vision.bigDataApi.model.InterfaceInfo;
import com.vision.bigDataApi.model.User;
import com.vision.bigDataApi.service.common.ResultService;
import com.vision.bigDataApi.service.mysql.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
//@RequestMapping("/user")
@Slf4j
@Validated
public class UserController {
    @Value("${pageNum}")
    private int pageNum;

    @Value("${pageSize}")
    private int pageSize;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private ResultService resultService;

    @GetMapping("/getAll")
    @ResponseBody
    public String getAll(String username) {
        return userMapper.getByUsername(username).toString();
    }

    @GetMapping("/logout")
    @ResponseBody
    public String logout(){
        SecurityUtils.getSubject().logout();
        return resultService.result(ReturnStatus.SUCCESS,null,"退出成功",null);
    }

    @RequestMapping("/notLogin")
    @ResponseBody
    public String pleaseLogin(){
        return resultService.result(ReturnStatus.SUCCESS,null,"请先登录再操作",null);
    }
    @PostMapping("/login")
    @ResponseBody
    public String login(HttpServletRequest request, User user) {
        if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())) {
            request.setAttribute("msg", "用户名或密码不能为空！");
            return resultService.result(ReturnStatus.SUCCESS,null,"用户名或密码不能为空",null);
        }

        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
            //如果已经登录
          return  resultService.result(ReturnStatus.SUCCESS,null,"已经登录",null);

        }
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());

        try {
            subject.login(token);
           return resultService.result(ReturnStatus.SUCCESS,null,"登录成功",null);
        } catch (LockedAccountException lae) {
            token.clear();
            return resultService.result(ReturnStatus.SUCCESS,null,"用户已经被锁定不能登录",null);
        } catch (AuthenticationException e) {
            token.clear();
            //request.setAttribute("msg", "用户或密码不正确！");
            //log.info("用户名或密码不真确");
            //eturn "login";
            return resultService.result(ReturnStatus.SUCCESS,null,"用户或密码不正确",null);
     }
    }

    //获取接口信息
    @GetMapping("/getByInterfaceInfoAndTabelInfo")
    @ResponseBody
    public String getByInterfaceInfoAndTabelInfo(@NotNull(message="interfaceId参数不能为null") Integer interfaceId, Integer pageNum, Integer pageSize){
        int pageN = 0;
        int pageS=0;
        if(pageNum!=null){
            pageN=pageNum;
        }
        else{
            pageN=this.pageNum;
        }
        if(pageSize!=null){
            pageS=pageSize;
        }
        else{
            pageS=this.pageSize;
        }
        PageHelper.startPage(pageN,pageS);
        List<HashMap<String, Object>> listMap = userService.getByInterfaceInfoAndTabelInfo(interfaceId);
        PageInfo pageInfo=new PageInfo(listMap);
        long total = pageInfo.getTotal();//总共多少条
        return resultService.result(ReturnStatus.SUCCESS,listMap,"成功",total);
    }

    /**
     * 获得接口日志信息
     */

    @GetMapping("/getLogInterfaceInfo")
    @ResponseBody
    public String getLogInterfaceInfo(Integer pageNum,Integer pageSize){
        int pageN = 0;
        int pageS=0;
        if(pageNum!=null){
            pageN=pageNum;
        }
        else{
            pageN=this.pageNum;
        }
        if(pageSize!=null){
            pageS=pageSize;
        }
        else{
            pageS=this.pageSize;
        }
        PageHelper.startPage(pageN,pageS);
        List<Map<String,Object>> listMap = userService.getByLogInterfaceInfo();
        PageInfo pageInfo=new PageInfo(listMap);
        long total = pageInfo.getTotal();//总共多少条
        return resultService.result(ReturnStatus.SUCCESS,listMap,"成功",total);
    }

    /**
     * 根据接口id统计接口数量  不传接口id统计的是所有接口的数量
     * @param iId
     * @return
     */
    @GetMapping("/getLogInterfaceInfoStat")
    @ResponseBody
    public String getLogInterfaceInfoStat(Integer interfaceId){
        List<Map<String, Object>> listMap = userService.getByLogInterfaceStat(interfaceId);
        return resultService.result(ReturnStatus.SUCCESS,listMap,"成功",null);
    }
}
